package com.test.learnkotlin.standard


fun main() {
    testTakeIf()

}

/*

https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/util/Standard.kt

使用 Kotlin 中的 takeIf
https://blog.csdn.net/feelinghappy/article/details/109676952

Kotlin里的takeIf和takeUnless
https://blog.csdn.net/totond/article/details/106070606

 */
internal fun testTakeIf() {
    val listPersonInfo = mutableListOf<PersonInfo?>()
    val size = 22
    for (index in 0 until size) {
        val number = index + 1
        val personInfo = PersonInfo(number, "kotlin_${number}")


        //看 takeIf 源码 ， 如果true返回本身，false 返回null
        personInfo.takeIf { it.age >= 18 }?.apply { adult = true }
        listPersonInfo.add(personInfo)
    }

    listPersonInfo.filter { it!!.age > 18 }.map {
//        println(it.toString())
        //PersonInfo(age=19, name=kotlin_19, adult=true)
        //PersonInfo(age=20, name=kotlin_20, adult=true)
        //PersonInfo(age=21, name=kotlin_21, adult=true)
        //PersonInfo(age=22, name=kotlin_22, adult=true)

    }

//    println(listPersonInfo)
    //[PersonInfo(age=1, name=kotlin_1, adult=false), PersonInfo(age=2, name=kotlin_2, adult=false), PersonInfo(age=3, name=kotlin_3, adult=false), PersonInfo(age=4, name=kotlin_4, adult=false),
// PersonInfo(age=5, name=kotlin_5, adult=false), PersonInfo(age=6, name=kotlin_6, adult=false), PersonInfo(age=7, name=kotlin_7, adult=false), PersonInfo(age=8, name=kotlin_8, adult=false),
// PersonInfo(age=9, name=kotlin_9, adult=false), PersonInfo(age=10, name=kotlin_10, adult=false), PersonInfo(age=11, name=kotlin_11, adult=false), PersonInfo(age=12, name=kotlin_12, adult=false),
// PersonInfo(age=13, name=kotlin_13, adult=false), PersonInfo(age=14, name=kotlin_14, adult=false), PersonInfo(age=15, name=kotlin_15, adult=false), PersonInfo(age=16, name=kotlin_16, adult=false),
// PersonInfo(age=17, name=kotlin_17, adult=false), PersonInfo(age=18, name=kotlin_18, adult=true), PersonInfo(age=19, name=kotlin_19, adult=true), PersonInfo(age=20, name=kotlin_20, adult=true),
// PersonInfo(age=21, name=kotlin_21, adult=true), PersonInfo(age=22, name=kotlin_22, adult=true)]


    listPersonInfo.map {
        it.takeUnless { // 返回 小于等于 18 的
            it?.age!! > 18
        }?.let {
            println(it)
            //PersonInfo(age=1, name=kotlin_1, adult=false)
            //PersonInfo(age=2, name=kotlin_2, adult=false)
            //PersonInfo(age=3, name=kotlin_3, adult=false)
            //PersonInfo(age=4, name=kotlin_4, adult=false)
            //PersonInfo(age=5, name=kotlin_5, adult=false)
            //PersonInfo(age=6, name=kotlin_6, adult=false)
            //PersonInfo(age=7, name=kotlin_7, adult=false)
            //PersonInfo(age=8, name=kotlin_8, adult=false)
            //PersonInfo(age=9, name=kotlin_9, adult=false)
            //PersonInfo(age=10, name=kotlin_10, adult=false)
            //PersonInfo(age=11, name=kotlin_11, adult=false)
            //PersonInfo(age=12, name=kotlin_12, adult=false)
            //PersonInfo(age=13, name=kotlin_13, adult=false)
            //PersonInfo(age=14, name=kotlin_14, adult=false)
            //PersonInfo(age=15, name=kotlin_15, adult=false)
            //PersonInfo(age=16, name=kotlin_16, adult=false)
            //PersonInfo(age=17, name=kotlin_17, adult=false)
            //PersonInfo(age=18, name=kotlin_18, adult=true)
        }
    }
}

data class PersonInfo(
    var age: Int = 1,
    var name: String? = "",
    var adult: Boolean = false


) {
    override fun toString(): String {
        return "PersonInfo(age=$age, name=$name, adult=$adult)"
    }
}

